package com.jiexin.datasource;

import com.alibaba.druid.pool.xa.DruidXADataSource;
import com.atomikos.icatch.jta.UserTransactionImp;
import com.atomikos.icatch.jta.UserTransactionManager;
import com.atomikos.jdbc.AtomikosDataSourceBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.jta.JtaTransactionManager;
import javax.sql.DataSource;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;
import java.sql.SQLException;

/**
 * @Author: Jay_Liu
 * @Description:  CAT数据库连接池配置类
 * @Date:
 * @Modified By:
 */
@Configuration
@MapperScan(basePackages = {"com.jiexin.dao.cat"}, sqlSessionTemplateRef  = "catSqlSessionTemplate")
public class CATDSConfig {

    @Bean(name = "catDataSource")
    public DataSource testDataSource(CATConfig catConfig) throws SQLException {
//        DruidXADataSource druidXADataSource = new DruidXADataSource();
//        druidXADataSource.setUrl(catConfig.getUrl());
//        druidXADataSource.setUsername(catConfig.getUsername());
//        druidXADataSource.setPassword(catConfig.getPassword());
//        druidXADataSource.setInitialSize(catConfig.getInitialSize());
//        druidXADataSource.setMinIdle(catConfig.getMinIdle());
//        druidXADataSource.setMaxActive(catConfig.getMaxActive());
//        druidXADataSource.setMaxWait(catConfig.getMaxWait());
//        druidXADataSource.setPoolPreparedStatements(catConfig.isPoolPreparedStatements());
//        druidXADataSource.setMaxPoolPreparedStatementPerConnectionSize(catConfig.getMaxPoolPreparedStatementPerConnectionSize());
//        druidXADataSource.setValidationQuery(catConfig.getValidationQuery());
//        druidXADataSource.setTestWhileIdle(catConfig.isTestWhileIdle());
//        //druidXADataSource.setRemoveAbandoned(catConfig.isRemoveAbandoned());
//        //druidXADataSource.setRemoveAbandonedTimeout(catConfig.getRemoveAbandonedTimeout());
//        druidXADataSource.setConnectionProperties(catConfig.getConnectionProperties());
//        //atomikos
//        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
//        atomikosDataSourceBean.setXaDataSource(druidXADataSource);
//        atomikosDataSourceBean.setUniqueResourceName("cat");
//        atomikosDataSourceBean.setMaxPoolSize(catConfig.getMaxPoolSize());
//        atomikosDataSourceBean.setBorrowConnectionTimeout(catConfig.getBorrowConnectionTimeout());

        DruidXADataSource druidXADataSource = new DruidXADataSource();
        druidXADataSource.setUrl(catConfig.getUrl());
        druidXADataSource.setUsername(catConfig.getUsername());
        druidXADataSource.setPassword(catConfig.getPassword());
        druidXADataSource.setInitialSize(catConfig.getInitialSize());
        druidXADataSource.setMinIdle(catConfig.getMinIdle());
        druidXADataSource.setMaxActive(catConfig.getMaxActive());
        druidXADataSource.setMaxWait(catConfig.getMaxWait());
        druidXADataSource.setPoolPreparedStatements(catConfig.isPoolPreparedStatements());
        druidXADataSource.setMaxPoolPreparedStatementPerConnectionSize(catConfig.getMaxPoolPreparedStatementPerConnectionSize());
        druidXADataSource.setValidationQuery(catConfig.getValidationQuery());
        druidXADataSource.setTestWhileIdle(catConfig.isTestWhileIdle());
        druidXADataSource.setRemoveAbandoned(catConfig.isRemoveAbandoned());
        druidXADataSource.setRemoveAbandonedTimeout(catConfig.getRemoveAbandonedTimeout());
        druidXADataSource.setConnectionProperties(catConfig.getConnectionProperties());

        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
        atomikosDataSourceBean.setXaDataSource(druidXADataSource);
        atomikosDataSourceBean.setUniqueResourceName("cat");
        atomikosDataSourceBean.setMaxPoolSize(catConfig.getMaxPoolSize());
        atomikosDataSourceBean.setBorrowConnectionTimeout(catConfig.getBorrowConnectionTimeout());
        return atomikosDataSourceBean;

    }

    @Bean(name = "catSqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("catDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/CAT/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "catSqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("catSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean(name = "catDataSourceJdbcTemplate")
    public JdbcTemplate catDataSourceJdbcTemplate( @Qualifier("catDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

}
